<template>
  <div>
    <el-dialog
      :title="title"
      :visible="visible"
      width="30%"
      :destroy-on-close="true"
      :before-close="close"
    >
      <el-form label-position="left" label-width="80px" :model="treeForm">
        <el-form-item label="编号">
          <el-input v-model="treeForm.code"></el-input>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="treeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="种类">
          <el-select v-model="treeForm.kindId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input type="Number" v-model="treeForm.age"></el-input>
        </el-form-item>
        <el-form-item label="所在市">
          <el-input v-model="treeForm.city"></el-input>
        </el-form-item>
        <el-form-item label="所在区">
          <el-input v-model="treeForm.area"></el-input>
        </el-form-item>
        <el-form-item label="地理位置">
          <el-input v-model="treeForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { createTree, updateTree } from "@/api/tree";
import { getTypeList } from "@/api/type";
export default {
  name: "Module",
  props: {
    title: {
      type: String,
      default: "新增",
    },
    visible: {
      type: Boolean,
      default: false,
    },
    treeInfo: {
      type: Object,
    },
  },
  data() {
    return {
      open: false,
      searchFrom: {
        currentPage: 1,
        pageSize: 100,
      },
      treeForm: {
        name: "",
        kindId: "",
        code: "",
        age: undefined,
        city: "",
        area: "",
        address: "",
      },
      options: [],
    };
  },

  watch: {
    title: {
      handler(newV) {
        if (newV == "编辑") {
          this.treeForm = this.treeInfo;
        } else {
          this.treeForm = {
            name: "",
            kindId: "",
            code: "",
            age: "",
            city: "",
            area: "",
            address: "",
          };
        }
      },
    },
    treeInfo: {
      handler(newV) {
        this.treeForm = newV;
      },
      deep: true,
      immediate: true,
    },
  },
  mounted() {
    this.getTypeList();
  },

  methods: {
    getTypeList() {
      getTypeList(this.searchFrom).then((res) => {
        console.log(res);
        this.options = res.data;
      });
    },
    close() {
      this.treeForm = {
        name: "",
        kindId: "",
        code: "",
        age: "",
        city: "",
        area: "",
        address: "",
      };
      this.$emit("cancel");
    },
    submit() {
      if (this.title == "新增") {
        console.log(this.treeForm);
        createTree(this.treeForm).then((res) => {
          this.$emit("getTreeList");
          this.close();
          console.log(res);
        });
      } else if (this.title == "编辑") {
        console.log(this.treeForm);
        updateTree(this.treeForm).then((res) => {
          console.log(res);
          this.$emit("getTreeList");
          this.close();
        });
      }
    },
  },
};
</script>
